package com.qf.drbackend.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.drbackend.dtos.DishDto;
import com.qf.drbackend.pojos.*;
import com.qf.drbackend.server.CategoryService;
import com.qf.drbackend.server.DishFlavorService;
import com.qf.drbackend.server.DishService;
import com.qf.drbackend.server.ShoppingCartService;
import com.qf.drbackend.utils.ThreadPoolUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public R list(Long categoryId,Integer status) throws ExecutionException, InterruptedException {

        Wrapper qw = Wrappers.lambdaQuery(Dish.class)
                .eq(Dish::getCategoryId,categoryId)
                .eq(Dish::getStatus,status);

        List<Dish> dishList = dishService.list(qw);

        //构建返回数据的集合
        List<DishDto> dishDtos = new ArrayList<>();

        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            //将dish中的数据，拷贝到dishDto
            /*
                对象属性拷贝的工具类
                    源对象（dish）的属性名和数据类型必须和目标对象（dishDto）的属性名和数据类型一致，才可以复制
             */
            BeanUtils.copyProperties(dish,dishDto);

            //获取当前登录用户的id
            String uid = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            //多线程优化，实现并发查询
            CompletableFuture.allOf(CompletableFuture.runAsync(()->{
                //查询菜品的口味 (根据菜品的id查询该菜品对应的口味 -- List)  异构：对模型进行改变，让他能满足页面需要
                Wrapper flavorQw = Wrappers.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> flavorList = dishFlavorService.list(flavorQw);
                dishDto.setFlavors(flavorList);
            }, ThreadPoolUtils.tp()),CompletableFuture.runAsync(()->{
                //菜品添加到购物车的数量(根据菜品的id+当前登录用户id查询 -- 数量)
                Wrapper cartQw = Wrappers.lambdaQuery(ShoppingCart.class)
                        //定义sql语句只查询number列
                        .select(ShoppingCart::getNumber)
                        .eq(ShoppingCart::getDishId,dish.getId())
                        .eq(ShoppingCart::getUserId,uid);
                ShoppingCart shoppingCart = shoppingCartService.getOne(cartQw);
                if (shoppingCart != null){
                    dishDto.setCopies(shoppingCart.getNumber());
                }
            }, ThreadPoolUtils.tp()),CompletableFuture.runAsync(()->{
                //菜品的分类名称 (根据分类id查询 -- 名称)
                Wrapper categoryQw = Wrappers.lambdaQuery(Category.class)
                        .select(Category::getName)
                        .eq(Category::getId,dish.getCategoryId());
                Category category = categoryService.getOne(categoryQw);
                dishDto.setCategoryName(category.getName());
            })).get();

            dishDtos.add(dishDto);
        }


        return R.success(dishDtos);
    }
}
